package org.esgi.repository.impl;

import java.io.File;

import org.esgi.util.FilePlatform;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSSerializer;

public class XMLRepository 
{
	private DOMImplementationLS impl_;

	public XMLRepository()
	{
		try 
		{
			DOMImplementationRegistry ir = DOMImplementationRegistry.newInstance();
			this.impl_ = (DOMImplementationLS) ir.getDOMImplementation("LS 3.0");
		} 
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public DOMImplementation getImpl() { return (DOMImplementation) this.impl_; }
	
	public Document parseFile(File f)
	{
		if(f != null)
		{
			LSParser lsp = this.impl_.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
			return lsp.parseURI(FilePlatform.getCrossPlatformPath(f));
		}
		return null;
	}
	
	public Boolean serializeDocument(Document d, File f)
	{
		if(d != null && f != null)
		{
			LSSerializer lss = this.impl_.createLSSerializer();
			return lss.writeToURI(d, FilePlatform.getCrossPlatformPath(f));
		}
		return false;
	}
}
